package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.CheckEntity;
import com.yz.common.entity.LogEntity;
import com.yz.common.entity.UserEntity;
import com.yz.common.utils.R;
import com.yz.common.validator.ValidatorUtils;
import com.yz.service.CheckService;
import com.yz.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 *数据审核
 *
 * @author yz

 *
 */
@RestController
@RequestMapping("/api")
@Api(tags="数据审核")
public class ApiCheckController {
    @Autowired
    private CheckService checkService;


    @GetMapping("getCheckList")
    @ApiOperation("获取数据审核列表")
    @Login
    public R getCheckList(@RequestHeader("token")String token,@RequestParam("order")int order,@RequestParam(value = "status",required = false) int status,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        String group = "create_at desc,id desc";
        if(order!=0){
            group = "create_at asc,id asc";
        }
        int total=0;
        Page<CheckEntity> logs=null;
        if(status==0){
            if(keyWord==null||keyWord.isEmpty()){
                total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId));
                logs = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId).orderBy(group));
            }else {
                total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId).like("number",keyWord).or().like("name_form",keyWord));
                logs = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("project_id",projectId).like("number",keyWord).or().like("name_form",keyWord).orderBy(group));
            }
        }else{
            if(keyWord==null||keyWord.isEmpty()){
                total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("status",status).eq("project_id",projectId));
                logs = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("status",status).eq("create_user",user.getUsername()).eq("project_id",projectId).orderBy(group));
            }else {
                total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("create_user",user.getUsername()).eq("status",status).eq("project_id",projectId).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')"));
                logs = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("status",status).eq("create_user",user.getUsername()).eq("project_id",projectId).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')").orderBy(group));
            }
        }
        return R.ok().put("data",logs.getRecords()).put("total",total);
    }
}
